package com.yangguang.huahui.ui.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.Reply;
import com.yangguang.huahui.mvp.presenter.ReplyListPresenter;
import com.yangguang.huahui.mvp.view.ReplyListView;
import com.yangguang.huahui.ui.adapters.QuestionListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/4/1.
 */

public class MyQuestionFragment extends BaseMvpFragment<ReplyListView, ReplyListPresenter> implements ReplyListView, SwipeRefreshLayout.OnRefreshListener{
    private int page;
    @BindView(R.id.mSwip)
    SwipeRefreshLayout mSwip;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private QuestionListAdapter mAdapter;
    private List<Reply.ListBean> datas = new ArrayList<>();
    @Override
    public void onRefresh() {
        fetchData();
    }

    @Override
    public void onSuccess(Reply reply) {
        if (reply.getList() != null){
            datas = reply.getList();
        }
        mAdapter.setNewData(datas);
        mSwip.setRefreshing(false);
    }

    @Override
    public void onError() {

    }

    @Override
    protected ReplyListPresenter initPresenter() {
        return new ReplyListPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getQuestionList(DataInfo.TOKEN);
    }

    @Override
    protected int initLayoutId() {
        return R.layout.frag_my_question;
    }

    @Override
    protected void initView() {
        mSwip.setColorSchemeResources(R.color.main_color, R.color.main_color, R.color.main_color);
        mSwip.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        //实现首次自动显示加载提示
        mSwip.post(new Runnable() {
            @Override
            public void run() {
                mSwip.setRefreshing(true);
            }
        });
        mAdapter = new QuestionListAdapter(R.layout.question_list_item, datas);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        //将适配器添加到RecyclerView
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {

    }
}
